//
//  LishiViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/8/23.
//

#import "LishiViewController.h"
#import "ZhenYanTableViewCell.h"
#import "ZhenYanDetailsViewController.h"
@interface LishiViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation LishiViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"浏览历史");
    // Do any additional setup after loading the view.
}

- (void)SQ_UIViewForSelfView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    [self SQ_tableViewAddRefresh];
}
- (void)SQ_viewWillAppear:(BOOL)animated{
  if (self.jsonArray.count<=0) {
      [self SQ_Header];
  }
}

- (void)SQ_Header{
  NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
  [dic setObject:@"1" forKey:@"current"];
  [dic setObject:@"20" forKey:@"size"];
  [HttpTool post:@"motto/history" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
      self.page = 1;
      [self.tableView.mj_header endRefreshing];
      NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
      NSArray * array = [MottoModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
      [self.jsonArray removeAllObjects];
      [self.jsonArray addObjectsFromArray:array];
      [self.tableView reloadData];
  } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
      [self.tableView.mj_header endRefreshing];
      [YJProgressHUD showMsgWithoutView:httpModel.message];
  }];
}
- (void)SQ_Foother{
  NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
  [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
  [dic setObject:@"20" forKey:@"size"];
  
  [HttpTool post:@"motto/history" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
      self.page ++;
      NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
      NSArray * array = [MottoModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
      [self.jsonArray addObjectsFromArray:array];
      [self.tableView reloadData];
      [self.tableView.mj_footer endRefreshing];
  } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
      [self.tableView.mj_footer endRefreshing];
  }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.jsonArray.count ;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZhenYanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZhenYanTableViewCell"];
    if (!cell) {
        cell = [[ZhenYanTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"ZhenYanTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray  objectAtIndex:indexPath.item];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [ZhenYanTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:30];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MottoModel * model = [self.jsonArray  objectAtIndex:indexPath.item];
    ZhenYanDetailsViewController * vc = [[ZhenYanDetailsViewController alloc]init];
    [self SQ_pushViewControllerFull:vc];
    WS(weakSelf);
    [vc setHideWritingouchBlock:^{
        [weakSelf.jsonArray removeObject:model];
        [weakSelf.tableView reloadData];
    }];
}

@end
